<?php
namespace app\modules\admin\controllers;

use app\models\ManagerLog;
use app\models\User;
use Yii;
use yii\data\Pagination;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;

/**
 * 用户管理
 *
 * Class UserController
 * @package app\modules\admin\controllers
 */
class UserController extends BaseController
{
    /**
     * 用户列表
     * @throws ForbiddenHttpException
     * @return string
     */
    public function actionList()
    {
        if (!$this->manager->can('user/list')) {
            throw new ForbiddenHttpException('没有权限。');
        }
        $query = User::find()->where(['status'=>[User::STATUS_ACTIVE, User::STATUS_STOPED]]);
        $query->andFilterWhere(['like', 'nickname', Yii::$app->request->get('search_nickname')]);
        $query->andFilterWhere(['like', 'mobile', Yii::$app->request->get('search_mobile')]);
        if (!empty(Yii::$app->request->get('search_type'))) {
            $query->andWhere(['&' . intval(Yii::$app->request->get('search_type')) . '>', 'type', 0]);
        }
        $pagination = new Pagination(['totalCount'=>$query->count()]);
        $model_list = $query->orderBy('id DESC')->offset($pagination->offset)->limit($pagination->limit)->all();
        return $this->render('list', [
            'model_list'=>$model_list,
            'pagination'=>$pagination
        ]);
    }

    /**
     * 用户编辑
     * @throws ForbiddenHttpException
     * @throws NotFoundHttpException
     * @return string
     */
    public function actionEdit()
    {
        if (!$this->manager->can('user/edit')) {
            throw new ForbiddenHttpException('没有权限。');
        }
        $id = Yii::$app->request->get('id');
        if ($id > 0) {
            $model = User::find()->where(['id'=>$id, 'status'=>[User::STATUS_ACTIVE, User::STATUS_STOPED]])->one();
            if (empty($model)) {
                throw new NotFoundHttpException('没有找到用户数据。');
            }
        } else {
            $model = new User();
            $model->type = User::TYPE_COMMON;
            if ($this->isPost()) {
                $model->type = Yii::$app->request->post('User')['type'];
            }
            $model->status = User::STATUS_ACTIVE;
            $model->create_time = time();
        }
        if ($model->load(Yii::$app->request->post())) {
            if (!empty(Yii::$app->request->post('User')['password'])) {
                $model->password = Yii::$app->security->generatePasswordHash(Yii::$app->request->post('User')['password']);
                if (!$model->isNewRecord) {
                    Yii::$app->session->addFlash('warning', '新密码已保存。');
                }
            }
            if ($model->save()) {
                ManagerLog::info($this->manager->id, '', '保存用户：' . print_r($model->attributes, true));
                Yii::$app->session->addFlash('success', '数据已保存。');
            }
        }
        return $this->render('edit', [
            'model'=>$model
        ]);
    }

    /**
     * 设置用户状态AJAX接口
     * @throws ForbiddenHttpException
     * @throws NotFoundHttpException
     * @return array
     */
    public function actionStatus()
    {
        if (!$this->manager->can('user/status')) {
            throw new ForbiddenHttpException('没有权限。');
        }
        $id = Yii::$app->request->get('id');
        /* @var $model User */
        $model = User::find()->where(['id'=>$id, 'status'=>[User::STATUS_ACTIVE, User::STATUS_STOPED]])->one();
        if (empty($model)) {
            throw new NotFoundHttpException('没有找到用户数据。');
        }
        $new_status = [User::STATUS_ACTIVE=>User::STATUS_STOPED, User::STATUS_STOPED=>User::STATUS_ACTIVE][$model->status];
        ManagerLog::info($this->manager->id, '', '修改用户状态：' . $model->id . ':' . $model->status . '->' . $new_status);
        $model->status = $new_status;
        $model->save();
        return [
            'result'=>'success'
        ];
    }

    /**
     * 删除用户AJAX接口
     * @throws ForbiddenHttpException
     * @throws NotFoundHttpException
     * @return array
     */
    public function actionDelete()
    {
        if (!$this->manager->can('user/status')) {
            throw new ForbiddenHttpException('没有权限。');
        }
        $id = Yii::$app->request->get('id');
        $model = User::findOne($id);
        if (empty($model)) {
            throw new NotFoundHttpException('没有找到用户数据。');
        }
        $model->status = User::STATUS_DELETED;
        $model->save(false);
        ManagerLog::info($this->manager->id, '', '删除用户：' . print_r($model->attributes, true));
        return [
            'result'=>'success'
        ];
    }
}
